package com.qipay.doc.controller;

import com.qipay.baas.model.ActionDoc;
import com.qipay.baas.model.ActionDocDetail;
import com.qipay.doc.service.ActionDocService;
import com.qipay.doc.vo.ActionDocDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DocController extends BaseController {

    @Autowired
    ActionDocService actionDocService;

    @Value("${doc.password}")
    protected String password;

    @RequestMapping(value = {"", "/index.jsp","/index"}, method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = createModelAndView(request,"index");
        List<ActionDoc> lst = actionDocService.selectActionDocByType((byte) 1);
        view.addObject("lst", lst);

        return view;
    }

    @RequestMapping(value = {"/doc/{docId:[\\d]+}"}, method = RequestMethod.GET)
    public ModelAndView detail(@PathVariable @NotNull int docId,
                               @RequestParam(value = "did", required = false, defaultValue = "0" ) int detailId,
                               HttpServletRequest request) {
        ModelAndView view = createModelAndView(request,"detail");
        HttpSession session = request.getSession();
        String isOn = (String) session.getAttribute("isOn");
        if(isOn==null || "off".equals(isOn)){
            return new ModelAndView("redirect:/");
        }

        List<ActionDoc> cateLst = actionDocService.selectActionDocByType((byte) 2);
        //List<ActionDocDetail> smartDetails = actionDocService.selectSmartActionDocDetailAll();
        List<ActionDocDetail> smartDetails = actionDocService.selectActionDocDetailByDocId(docId);
        ActionDocDetail currentDoc = null;
        if (cateLst != null && cateLst.size() > 0) {
            List<ActionDocDTO> lst = new ArrayList<>(cateLst.size());
            for (ActionDoc doc : cateLst) {
                ActionDocDTO dto = new ActionDocDTO();
                BeanUtils.copyProperties(doc, dto);
                //List<ActionDocDetail> details = actionDocService.selectActionDocDetailByDocId(doc.getId());
                if (smartDetails != null && smartDetails.size() > 0) {
                    dto.setChild(new ArrayList<>());
                    for (ActionDocDetail detail : smartDetails) {
                        if (detail.getPid() == doc.getId()) {
                            dto.getChild().add(detail);
                        }
                        if (detail.getId() == detailId) {
                            dto.setCurrentDetailId(detailId);
                            currentDoc = actionDocService.selectActionDocDetailByPK(detailId);
                        }
                    }
                    lst.add(dto);
                }
            }
            if (currentDoc == null) {
                if (smartDetails != null) {
                    currentDoc =actionDocService.selectActionDocDetailByPK(smartDetails.get(0).getId());
                }
            }
            view.addObject("lst", lst);
            view.addObject("detail", currentDoc);
            view.addObject("currentDocId",docId);
            return view;
        }

        return new ModelAndView("redirect:/");
    }


    @RequestMapping(value = { "/codeUpdate"}, method = RequestMethod.GET)
    public ModelAndView codeUpdate(HttpServletRequest request) {
        ModelAndView view = createModelAndView(request,"code-update");
        List<ActionDoc> lst = actionDocService.selectActionDocByType((byte) 1);
        view.addObject("lst", lst);

        HttpSession session = request.getSession();
        String isOn = (String) session.getAttribute("isOn");
        if("off".equals(isOn)){
            return new ModelAndView("redirect:/");
        }
        return view;
    }

    @RequestMapping(value = { "/yan"}, method = RequestMethod.GET)
    public ModelAndView yan(
            HttpServletRequest request,
            @RequestParam(value = "pwd", required = false, defaultValue = "") String pwd
    ) {
        ModelAndView view = createModelAndView(request,"index");
        List<ActionDoc> lst = actionDocService.selectActionDocByType((byte) 1);
        view.addObject("lst", lst);
        HttpSession session = request.getSession();
        if(password.equals(pwd)){
            view.addObject("isOn", "on");
            session.setAttribute("isOn","on");
        }else{
            view.addObject("isOn", "off");
        }

        return view;
    }
}
